#include "playinterface.h"
#include "ui_playinterface.h"

playinterface::playinterface(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::playinterface)
{
    ui->setupUi(this);
    resize(1280,720);

    setWindowTitle("大富翁");
    QIcon windowicon(":/image/icon.jfif");
    setWindowIcon(windowicon);

    //设置qss样式
    QFile file(":/qss/playinterface.qss"); //通过文件路径创建文件对象
    file.open(QFile::ReadOnly); //文件打开方式
    QString str = file.readAll(); //获取qss中全部字符
    this->setStyleSheet(str); //设置样式表

    //背景图片
    QPalette PAllbackground = this->palette();
    QImage ImgAllbackground(":/image/country.jfif");
    QImage fitimgpic=
            ImgAllbackground.scaled(this->width(),
                                    this->height(), Qt::IgnoreAspectRatio);
    PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
    this->setPalette(PAllbackground);

    //设置按钮
    setbackground = new QComboBox(this);
    setbackground->setObjectName("setbackground");
    setbackground->setView(new QListView());
    setbackground->setGeometry(1150,25,100,40);
    setbackground->addItem("  乡村",1);
    setbackground->addItem("  城市",2);
    setbackground->addItem("  沙漠",3);
    setbackground->addItem("  森林",4);
    setbackground->addItem("  冰雪",5);

    //设置不同背景下地图的颜色及边框颜色
    countrycolor = "rgb(255,235,205,200)";
    citycolor = "rgb(190,190,190,200)";
    forestcolor = "rgb(238,197,145,200)";
    desertcolor = "rgb(202,255,112,200)";
    icecolor = "rgb(141,238,238,200)";

    countrybordercolor = "rgb(255,222,173)";
    citybordercolor = "rgb(105,105,105)";
    desertbordercolor = "rgb(67,205,128)";
    forestbordercolor = "rgb(205,133,63)";
    icebordercolor = "rgb(135,206,255)";

    //设置切换背景
    connect(setbackground, QOverload<int>::of(&QComboBox::currentIndexChanged),
        [=](int index){
        switch (index) {
        case 0:{
            QPalette PAllbackground = this->palette();
            QImage ImgAllbackground(":/image/country.jfif");
            QImage fitimgpic=
                    ImgAllbackground.scaled(this->width(),
                                            this->height(), Qt::IgnoreAspectRatio);
            PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
            this->setPalette(PAllbackground);
            for(int i=0;i<10;i++){
                for(int j=0;j<10;j++){
                    if(map[i][j].exist){
                        QString mapsheet=QString("QTextBrowser{background-color:%1;border-radius:10px;border:8px solid %2;}").arg(countrycolor,countrybordercolor);
                        map[i][j].node->setStyleSheet(mapsheet);
                    }
                }
            }
            break;
        }
        case 1:{
            QPalette PAllbackground = this->palette();
            QImage ImgAllbackground(":/image/city.jfif");
            QImage fitimgpic=
                    ImgAllbackground.scaled(this->width(),
                                            this->height(), Qt::IgnoreAspectRatio);
            PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
            this->setPalette(PAllbackground);
            for(int i=0;i<10;i++){
                for(int j=0;j<10;j++){
                    if(map[i][j].exist){
                        QString mapsheet=QString("QTextBrowser{background-color:%1;border-radius:10px;border:8px solid %2;}").arg(citycolor,citybordercolor);
                        map[i][j].node->setStyleSheet(mapsheet);
                    }
                }
            }
            break;
        }
        case 2:{
            QPalette PAllbackground = this->palette();
            QImage ImgAllbackground(":/image/desert.jfif");
            QImage fitimgpic=
                    ImgAllbackground.scaled(this->width(),
                                            this->height(), Qt::IgnoreAspectRatio);
            PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
            this->setPalette(PAllbackground);
            for(int i=0;i<10;i++){
                for(int j=0;j<10;j++){
                    if(map[i][j].exist){
                        QString mapsheet=QString("QTextBrowser{background-color:%1;border-radius:10px;border:8px solid %2;}").arg(desertcolor,desertbordercolor);
                        map[i][j].node->setStyleSheet(mapsheet);
                    }
                }
            }
            break;
        }
        case 3:{
            QPalette PAllbackground = this->palette();
            QImage ImgAllbackground(":/image/forest.jfif");
            QImage fitimgpic=
                    ImgAllbackground.scaled(this->width(),
                                            this->height(), Qt::IgnoreAspectRatio);
            PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
            this->setPalette(PAllbackground);
            for(int i=0;i<10;i++){
                for(int j=0;j<10;j++){
                    if(map[i][j].exist){
                          QString mapsheet=QString("QTextBrowser{background-color:%1;border-radius:10px;border:8px solid %2;}").arg(forestcolor,forestbordercolor);
                        map[i][j].node->setStyleSheet(mapsheet);
                    }
                }
            }
            break;
        }
        case 4:{
            QPalette PAllbackground = this->palette();
            QImage ImgAllbackground(":/image/ice.jpg");
            QImage fitimgpic=
                    ImgAllbackground.scaled(this->width(),
                                            this->height(), Qt::IgnoreAspectRatio);
            PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
            this->setPalette(PAllbackground);
            for(int i=0;i<10;i++){
                for(int j=0;j<10;j++){
                    if(map[i][j].exist){
                        QString mapsheet=QString("QTextBrowser{background-color:%1;border-radius:10px;border:8px solid %2;}").arg(icecolor,icebordercolor);
                        map[i][j].node->setStyleSheet(mapsheet);
                    }
                }
            }
            break;
        }
        }
    });

    //设置地图
    setmap();

    //设置玩家
    setplayer();
}

playinterface::~playinterface()
{
    delete ui;
}

void playinterface::setmap()
{
    //绘制地图
    for(int i=0;i<10;i++){
        map[0][i].x = 100 + 100*i;
        map[0][i].y = 80;
        map[0][i].exist = true;

        map[9][i].x = 100 + 100*i;
        map[9][i].y = 580;
        map[9][i].exist = true;
    }

    for(int i=1;i<5;i++){
        map[i][0].x = 100;
        map[i][0].y = 100*i+80;
        map[i][0].exist = true;

        map[i][9].x = 1000;
        map[i][9].y = 100*i+80;
        map[i][9].exist = true;
    }

    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            if(map[i][j].exist){
                map[i][j].node = new QTextBrowser(this);
                map[i][j].node->setGeometry(map[i][j].x,map[i][j].y,80,80);
                QString mapsheet=QString("QTextBrowser{background-color:%1;border-radius:10px;border:8px solid %2;}").arg(countrycolor,countrybordercolor);
                map[i][j].node->setStyleSheet(mapsheet);
            }
        }
    }

    map[0][0].node->setText("开始");
}

void playinterface::setplayer()
{
    playerlist[1].exist = true;

    for(int i=1;i<9;i++){
        playerlist[i].order = i;
        if(playerlist[i].exist){
            playerlist[i].x = map[0][0].x-10;
            playerlist[i].y = map[0][0].y;
            playerlist[i].player = new QLabel(this);
            playerlist[i].player->setGeometry(playerlist[i].x,playerlist[i].y,100,100);
            QString playersheet=QString("QLabel{border-image:url(:/image/playerimage%1.png);}")
                    .arg(i);
            playerlist[i].player->setStyleSheet(playersheet);
        }
    }
}

void playinterface::move(Player *_player, mapnode destination)
{
    animation = new QPropertyAnimation(_player->player,"geometry");
    _player->moveplayer(&destination,animation);
}

void playinterface::on_pushButton_clicked()
{
    move(&playerlist[1],map[0][9]);
}
